/* Copyright (C) Abu Rizal, 2009.
 * 
 * This file is part of QurText.
 * 
 * QurText is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * QurText is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with QurText. If not, see <http://www.gnu.org/licenses/>.
 */
package qurtext.client;

import java.util.TreeMap;

import com.google.gwt.user.client.rpc.AsyncCallback;

public class SectionPresenter {
	private SectionView sectionView;
	private QurTextServiceAsync qurTextService;
	private MainPresenter mainPresenter;

    private ClientVerse basmallah;

	public MainPresenter getMainPresenter() {
		return mainPresenter;
	}

	public void setMainPresenter(MainPresenter mainPresenter) {
		this.mainPresenter = mainPresenter;
	}

	public QurTextServiceAsync getQurTextService() {
		return qurTextService;
	}

	public void setQurTextService(QurTextServiceAsync qurTextService) {
		this.qurTextService = qurTextService;
	}

	public void showSection(ClientSection section) {
		sectionView.showSection(section);
	}

	public void setSectionView(SectionView sectionView) {
		this.sectionView = sectionView;
	}

	public SectionView getSectionView() {
		return sectionView;
	}

	public void updateSectionPanel(final ClientSection section) {
		qurTextService.getSectionVerseList(section.chapter, section.verse, section.endVerse, new AsyncCallback<TreeMap<Integer,ClientVerse>>() {

			@Override
			public void onFailure(Throwable caught) {
				mainPresenter.handleError(caught);
			}

			@Override
			public void onSuccess(TreeMap<Integer, ClientVerse> verses) {
				sectionView.sectionPanelClear();
				section.verses=verses;
				sectionView.showTranslation(verses);
				sectionView.showText(verses);
				mainPresenter.getUserPresenter().processUserProgress(section);
			}

		});
	}

	public void onSectionPresenterLoad() {
		qurTextService.getBasmallah(new AsyncCallback<ClientVerse>(){

			@Override
			public void onFailure(Throwable caught) {
				mainPresenter.handleError(caught);
			}

			@Override
			public void onSuccess(ClientVerse result) {
				setBasmallah(result);
			}
			
		});
	}

	public void setBasmallah(ClientVerse basmallah) {
		this.basmallah = basmallah;
	}

	public ClientVerse getBasmallah() {
		return basmallah;
	}

	public void onInitSection() {
		final ClientSection section=mainPresenter.getCurrentSection();
		qurTextService.initSectionVerses(section.chapter, section.verse, new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				mainPresenter.handleError(caught);
			}

			@Override
			public void onSuccess(Void result) {
	    		updateSectionPanel(section);
			}
		});
	}

	public void onInitLiteral() {
		final ClientSection section=mainPresenter.getCurrentSection();
		String start="" + section.chapter + ":" + section.verse + ":0";
		initLiteral(start);
	}

	private void initLiteral(String start) {
		sectionView.setStatusText("Updating " + start);
		qurTextService.initSectionLiterals(start, new AsyncCallback<String>() {

			@Override
			public void onFailure(Throwable caught) {
				mainPresenter.handleError(caught);
			}

			@Override
			public void onSuccess(String result) {
				if (null==result) {
					final ClientSection section=mainPresenter.getCurrentSection();
					updateSectionPanel(section);
					sectionView.setStatusText("Click a button.");
				} else {
					initLiteral(result);
				}
			}
		});
	}
}
